package com.person.jc.web.base.standard;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.person.jc.service.base.device.BdDeviceRelationService;
import com.person.jc.service.base.equip.BdEquipmentService;
import com.person.jc.service.base.standard.BdStandardDeviceService;
import com.person.jc.service.base.standard.BdStandardMatterService;
import com.person.jc.service.base.standard.BdStandardMeasureScopeService;
import com.person.jc.utils.Servlets;
import com.person.jc.utils.Utils;
import com.person.jc.web.base.device.BdDeviceRelationController;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jetty.util.StringUtil;

public class BdStandardMeasureScopeController extends Controller {
	
	public void init(){
		setAttr("menuId", getPara("menuId"));
		render("main.jsp");
	}
	
	public void list(){
		Map<String,Object> map = Servlets.getParametersStartingWith(getRequest(), "p_");
		List<Record> list = BdStandardMeasureScopeService.me.getList(map);
		setAttr("list", list);
		render("list.jsp");
	}
	
	public void edit(){
		String scopeId = getPara("scopeId");
		String view = getPara("view");
		if(Utils.isNotEmpty(scopeId)){
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("scopeId", scopeId);
			List<Record> list = BdStandardMeasureScopeService.me.getList(map);
			if(list.size() != 0){
				setAttr("obj", list.get(0));
			}
		}
		setAttr("view", view);
		render("edit.jsp");
	}
	
	public void save(){
		String res = "success";
		Map<String,Object> map = Servlets.getParametersStartingWith(getRequest(), "p_");
		String scopeId = String.valueOf(map.get("scopeId"));
		try {
			if(Utils.isNotEmpty(scopeId)){
				BdStandardMeasureScopeService.me.update(map);
			}else{
				BdStandardMeasureScopeService.me.save(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
			res = "error";
		}
		renderText(res);
	}
	
	public void delete(){
		String res = "success";
		String scopeIds = getPara("scopeIds");
		try {
			for (String scopeId : scopeIds.split(",")) {
				BdStandardMeasureScopeService.me.delete(scopeId);
			}
		} catch (Exception e) {
			e.printStackTrace();
			res = "error";
		}
		renderText(res);
	}

	public void showTemplateConfigure(){ //todo配置模板
		String scopeId = getPara("scopeId");
		Map<String,Object> map=new HashMap<String,Object>();
		if(scopeId!=null){
			map=BdStandardMeasureScopeService.me.getTemplateHeardData(Integer.valueOf(scopeId));
			//获得已经配置好的配置数据
			List<Record> rs=BdDeviceRelationService.me.getConfigureItemDataByScopeId(Integer.valueOf(scopeId));
			setAttr("rs", JsonKit.toJson(rs));
		}
		setAttr("scopeId",scopeId);
		setAttr("map", map);
		render("editTemplate.jsp");
	}

	public void selectBzwz(){ //TODO 选择标准物质
		String ids=getPara("standardMatterIds");
		Map<String,Object> map=new HashMap<String,Object>();
		List<Record> list=new ArrayList<Record>();
		if(StringUtils.isNotEmpty(ids)){
			map.put("standardMatterIds",ids);
			list = BdStandardMatterService.me.getList(map);
		}
		renderJson (list);
	}

	public void selectJlbzzz(){ //TODO 选择计量标准装置
		String ids=getPara("deviceIds");
		Map<String,Object> map=new HashMap<String,Object>();
		List<Record> list=new ArrayList<Record>();
		if(StringUtils.isNotEmpty(ids)){
			map.put("deviceIds",ids);
			list = BdStandardDeviceService.me.getList(map);
		}
		renderJson (list);

	}

	public void selectJlzz(){ //TODO 选择计量标准装置
		String ids=getPara("equIds");
		Map<String,Object> map=new HashMap<String,Object>();
		List<Record> list=new ArrayList<Record>();
		if(StringUtils.isNotEmpty(ids)){
			map.put("equIds",ids);
			list = BdEquipmentService.me.getList(map);
		}
		renderJson (list);

	}

	@Before(Tx.class)
	public void saveConfigureItem(){ //TODO 保存模板配置信息
		String scopeId=getPara("scopeId");
		BdDeviceRelationService.me.delConfigureItemData(Integer.valueOf(scopeId));


		String[] bzwzMatterIdsids=getParaValues("bzwzMatterId");
		String[] bzwzsorts=getParaValues("bzwzSort");

		if(bzwzMatterIdsids.length>0){
			for(int i=0;i<bzwzMatterIdsids.length;i++){
				Map<String,Object> map=new HashMap<String,Object>();
				map.put("deviceType","0");
				map.put("deviceId",bzwzMatterIdsids[i]);
				map.put("scopeId",scopeId);
				if(StringUtils.isNotEmpty(bzwzsorts[i])){
					map.put("sort",bzwzsorts[i]);
				}else{
					map.put("sort",0);

				}
				BdDeviceRelationService.me.saveConfigureItemData(map);
			}
		}

		String[] jlbzzzDeviceIds=getParaValues("jlbzzzDeviceId");
		String[] jlbzzzSorts=getParaValues("jlbzzzSort");

		if(jlbzzzDeviceIds.length>0){
			for(int i=0;i<jlbzzzDeviceIds.length;i++){
				Map<String,Object> map=new HashMap<String,Object>();
				map.put("deviceType","1");
				map.put("deviceId",jlbzzzDeviceIds[i]);
				map.put("scopeId",scopeId);
				if(StringUtils.isNotEmpty(jlbzzzSorts[i])){
					map.put("sort",jlbzzzSorts[i]);
				}else{
					map.put("sort",0);

				}
				BdDeviceRelationService.me.saveConfigureItemData(map);
			}
		}


		String[] jlzzDeviceIds=getParaValues("jlzzDeviceId");
		String[] jlzzSorts=getParaValues("jlzzSort");

		if(jlzzDeviceIds.length>0){
			for(int i=0;i<jlzzDeviceIds.length;i++){
				Map<String,Object> map=new HashMap<String,Object>();
				map.put("deviceType","2");
				map.put("deviceId",jlzzDeviceIds[i]);
				map.put("scopeId",scopeId);
				if(StringUtils.isNotEmpty(jlzzSorts[i])){
					map.put("sort",jlzzSorts[i]);
				}else{
					map.put("sort",0);

				}
				BdDeviceRelationService.me.saveConfigureItemData(map);
			}
		}




		showTemplateConfigure();
	}
}
